package com.itic.ht.mem.withdraw.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.ht.mem.member.persistence.model.WebMember;
import com.itic.ht.mem.member.service.WebMemberService;
import com.itic.ht.mem.performance.persistence.model.Performance;
import com.itic.ht.mem.performance.persistence.model.PerformanceExample;
import com.itic.ht.mem.withdraw.persistence.dao.WithdrawDao;
import com.itic.ht.mem.withdraw.persistence.model.Withdraw;
import com.itic.ht.mem.withdraw.persistence.model.WithdrawExample;

@Service
@Transactional
public class WithdrawService {
	
	// return code
	private static final String SUCCESS = ResponseCode.SUCCESS.getCode();
    private static final String FAILED = ResponseCode.SERVICE_ERROR.getCode();
    private static final String WITHDRAW_LODING = "1";
    
	@Autowired
	private WithdrawDao withdrawDao;
	@Autowired
	private WebMemberService webMemberService;

	public Withdraw get(String id) {
		if (StringHelper.isNotEmpty(id)) {
            return withdrawDao.selectByPrimaryKey(id);
        }
        return null;
	}

	public String add(Withdraw templet) {
		templet.setStatus(WITHDRAW_LODING);
		int code = withdrawDao.insert(templet);
        if (1 == code) {
            return SUCCESS;
        } else if (0 == code) {
            return FAILED;
        } else {
            return FAILED;
        }
	}

	public int count(WithdrawExample example) {
		return withdrawDao.countByExample(example);
    }

	public  List<Withdraw> list(WithdrawExample example) {
		return withdrawDao.selectByExample(example);
	}

	public  String edit(Withdraw templet) {
		int code = 0;
        String id = templet.getId();
        if (StringHelper.isNotEmpty(id.toString().trim())) {
            if (null == templet.getMtime()) {
            	templet.setMtime(new Date());
            }
            if(templet.getStatus().equals("0")){
            	//将用户余额返还
            	WebMember member = webMemberService.getMemberByAccount(templet.getAccount());
            	member.setBalance(new BigDecimal(templet.getChangeAcount()+member.getBalance().doubleValue()));
            	templet.setBalance(templet.getChangeAcount()+member.getBalance().doubleValue());
            	webMemberService.edit(member);
            }
            code = withdrawDao.updateByPrimaryKeySelective(templet);
        }
        if (1 == code) {
            return SUCCESS;
        } else if (0 == code) {
            return FAILED;
        } else {
            return FAILED;
        }
	}

	public String deleteByExample(Withdraw templet, WithdrawExample example) {
		int code = 0;
        if (null == templet.getMtime()) {
        	templet.setMtime(new Date());
        }
        if (null == templet.getIsEffective() || StringHelper.isBlank(templet.getIsEffective())) {
        	templet.setIsEffective(DBConst.FALSE);
        }
        code = withdrawDao.updateByExampleSelective(templet, example);
        if (code > 0) {
            return SUCCESS;
        } else {
            return FAILED;
        }
	}


}
